﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using SAAC.DTO;
using System.Data;


namespace SAAC.DAL
{
	public class ExtensaoDAL : DALBase
	{

		public ExtensaoDTO Select(ExtensaoDTO dto, string order = null)
		{
			try
			{
				string sql = "CALL sp_extensao_sel(@id,@orientador,@declaracao,@relatorioFinal,@order,@limit);";

				if (dto.Declaracao == null) dto.Declaracao = new AnexoDTO();
				if (dto.RelatorioFinal == null) dto.RelatorioFinal = new AnexoDTO();

				MySqlCommand cmd = new MySqlCommand(sql, con);
				cmd.Parameters.AddWithValue("@id", dto.Id);
				cmd.Parameters.AddWithValue("@orientador", dto.Orientador);
				cmd.Parameters.AddWithValue("@declaracao", dto.Declaracao.Id);
				cmd.Parameters.AddWithValue("@relatorioFinal", dto.RelatorioFinal.Id);
				cmd.Parameters.AddWithValue("@order", order);
				cmd.Parameters.AddWithValue("@limit", 1);

				con.Open();
				MySqlDataReader dr;

				dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

				if (dr.Read())
				{
					dto.Id = Convert.ToInt32(dr["id"]);
					dto.Orientador = dr["orientador"].ToString();
					dto.Declaracao.Id = Convert.ToInt32(dr["declaracao"]);
					dto.RelatorioFinal.Id = Convert.ToInt32(dr["relatorioFinal"]);
				}
				else dto = null;

				return dto;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public List<ExtensaoDTO> SelectAll(ExtensaoDTO dto, string order = null, int limit = 1000)
		{
			try
			{
				string sql = "CALL sp_extensao_sel(@id,@orientador,@declaracao,@relatorioFinal,@order,@limit);";

				var lista = new List<ExtensaoDTO>();

				MySqlCommand cmd = new MySqlCommand(sql, con);
				cmd.Parameters.AddWithValue("@id", dto.Id);
				cmd.Parameters.AddWithValue("@orientador", dto.Orientador);
				cmd.Parameters.AddWithValue("@declaracao", dto.Declaracao.Id);
				cmd.Parameters.AddWithValue("@relatorioFinal", dto.RelatorioFinal.Id);
				cmd.Parameters.AddWithValue("@order", order);
				cmd.Parameters.AddWithValue("@limit", limit);

				con.Open();
				MySqlDataReader dr;

				dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

				while (dr.Read())
				{
					var item = new ExtensaoDTO();
					item.Id = Convert.ToInt32(dr["id"]);
					item.Orientador = dr["orientador"].ToString();
					item.Declaracao.Id = Convert.ToInt32(dr["declaracao"]);
					item.RelatorioFinal.Id = Convert.ToInt32(dr["relatorioFinal"]);
					lista.Add(item);
				}

				return lista;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}


		public void Insert(ExtensaoDTO dto)
		{
			try
			{
				String sql = @"CALL sp_extensao_ins(@id,@orientador,@declaracao,@relatorioFinal);";

				MySqlCommand cmd = new MySqlCommand(sql, con);

				cmd.Parameters.AddWithValue("@id", dto.Id);
				cmd.Parameters.AddWithValue("@orientador", dto.Orientador);
				cmd.Parameters.AddWithValue("@declaracao", dto.Declaracao.Id);
				cmd.Parameters.AddWithValue("@relatorioFinal", dto.RelatorioFinal.Id);

				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public void Update(ExtensaoDTO dto)
		{
			try
			{
				String sql = "CALL sp_extensao_upd(@id,@orientador,@declaracao,@relatorioFinal);";

				MySqlCommand cmd = new MySqlCommand(sql, con);

				cmd.Parameters.AddWithValue("@id", dto.Id);
				cmd.Parameters.AddWithValue("@orientador", dto.Orientador);
				cmd.Parameters.AddWithValue("@declaracao", dto.Declaracao.Id);
				cmd.Parameters.AddWithValue("@relatorioFinal", dto.RelatorioFinal.Id);

				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public void Delete(ExtensaoDTO dto)
		{
			try
			{
				String sql = "CALL sp_extensao_del(@id);";

				MySqlCommand cmd = new MySqlCommand(sql, con);

				cmd.Parameters.AddWithValue("@id", dto.Id);
				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}
	}
}
